package com.spr.service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.spr.model.User;

@Service
public class UserDetailsServiceImpl implements UserDetailsService{
	
    private EntityManager entityManager;
    @PersistenceContext
    public void setEntityManager(EntityManager newEm){
        this.entityManager = newEm;
    }

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {

	       return entityManager.createQuery("from User where username = :username", User.class)
                   .setParameter("username", username)
                   .getSingleResult();
	}

}
